package org.jdesktop.swingx.table.model;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

public abstract class AbstractTableModelWapper<T extends TableModel> implements TableModel,TableModelListener {
	protected T t;
    boolean isWapperModel=false;
	public AbstractTableModelWapper(T t) {
		this.t=t;
		if(t instanceof AbstractTableModelWapper)
			isWapperModel=true;
		t.addTableModelListener(this);
	}

	//-----------------------------------------------------------------
	//包装类函数,只对包装对象操作
	
	public int getRowCount() {
		return t.getRowCount();
	}

	
	public int getColumnCount() {
		return t.getColumnCount();
	}
	
	public Object getValueAt(int rowIndex, int columnIndex) {
		return t.getValueAt(getRowIndex(rowIndex), getColumnIndex(columnIndex));
	}


	@Override
	public String getColumnName(int columnIndex) {
		return t.getColumnName(getColumnIndex(columnIndex));
	}


	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return t.getColumnClass(getColumnIndex(columnIndex));
	}


	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return t.isCellEditable(getRowIndex(rowIndex), getColumnIndex(columnIndex));
	}


	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		t.setValueAt(aValue, getRowIndex(rowIndex), getColumnIndex(columnIndex));	
	}


	@Override
	public void addTableModelListener(TableModelListener l) {
		t.addTableModelListener(l);
	}


	@Override
	public void removeTableModelListener(TableModelListener l) {
		t.removeTableModelListener(l);
	}
	
	//当前包装 相对于上一级的行列变换
	public abstract int getRowIndex(int wapperRowIndex);
	
	public abstract int getColumnIndex(int wapperColumnIndex);
	
	
	//---------------------------------------------------------------
	/**
	*下面这个是相对于原始对象，因有可能经过多级包装
	*@return -1;表明是新增的
	*/
	public int getRealRowNumber(int wapperRowNum){
        if(isWapperModel){
        	return ((AbstractTableModelWapper)t).getRealRowNumber(wapperRowNum);
        }
		return wapperRowNum;
	}
	
	//return -1;表明是新增的
	public  int getRealColumnNumber(int wapperColumnNumber){
		 if(isWapperModel){
	        	return ((AbstractTableModelWapper)t).getRealColumnNumber(wapperColumnNumber);
		 }
		return wapperColumnNumber;
	}
	
	//------------------------------------------------------------
	//工具函数
	public boolean isRealRowNumber(int wapperRowNum){
		return  wapperRowNum==getRealRowNumber(wapperRowNum);
	}
	
	public boolean isRealColumnNumber(int wapperColumnNumber){
		return wapperColumnNumber==getRealColumnNumber(wapperColumnNumber);
	}
	
	
	//返回 -1;表明可能是由于包装给隐藏了。
	public int getWapperRowNumber(int realRowNumber){
		for(int i=0;i<this.getRowCount();i++){
			if(realRowNumber==getRealRowNumber(i))
				return i;
		}
		return -1;
	}

	//返回 -1;表明可能是由于包装给隐藏了。
	public int getWapperColumnNumber(int realColumnNumber){
		for(int i=0;i<this.getColumnCount();i++){
			if(realColumnNumber==getRealColumnNumber(i))
				return i;
		}
		return -1;
	}

	@Override
	public void tableChanged(TableModelEvent e) {	
	}
	
}
